package com.myown.web;

import com.myown.constant.ConstantInfo;
import com.myown.dto.json.JsonResult;
import com.myown.model.User;
import com.myown.util.JCaptchaInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import java.io.ByteArrayOutputStream;
import java.util.Date;

/**
 * 提供通用接口 <p>
 * @author lincky
 * @version v1.0.0
 */
@Controller
@RequestMapping("/common")
public class CommonController extends BaseController{
    // 日志记录对象
    private final static Logger logger = LoggerFactory.getLogger(CommonController.class);

    private static int count = 0;

    // 验证码工具类注入
    @Autowired
    private JCaptchaInfo jCaptchaInfo;

    /**
     * 获取系统时间 <p>
     * @return JsonResult封装的系统时间（ms）
     */
    @RequestMapping(value = "/time/now",
            method = RequestMethod.GET,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public JsonResult<Long> now() {
        return new JsonResult<>(new Date().getTime());
    }

    /**
     * 验证码图片获取 80*32 <p>
     * @throws Exception 将异常抛向{@link CommonExceptionController}处理
     */
    @RequestMapping(value = "/captcha/image",
            method = RequestMethod.GET)
    public void captchaImage() throws Exception {
        response.setHeader("Cache-Control", "no-store");
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires", 0L);
        response.setContentType("image/jpeg");

        ByteArrayOutputStream jpegOutputStream = jCaptchaInfo.getJpegOutputStream(request.getSession().getId(),
                request.getLocale());

        ServletOutputStream servletOutputStream = response.getOutputStream();
        servletOutputStream.write(jpegOutputStream.toByteArray());
        servletOutputStream.flush();
        servletOutputStream.close();
    }

    @RequestMapping(value = "/test/login/{logonname}")
    public String loginTest(@PathVariable("logonname") String logonname) throws Exception {
        session.setAttribute(ConstantInfo.Account.HTTP_GLOBAL_ACCOUNT, new User(logonname));
        System.out.println(logonname + " login id:" + session.getId());
        return "/singleLogin";
    }

    @RequestMapping(value = "/test/login/check")
    public String loginCheck() throws Exception {
        User user = (User) session.getAttribute(ConstantInfo.Account.HTTP_GLOBAL_ACCOUNT);
        if (user == null) {
            System.out.println("null check id:" + session.getId());
        } else {
            System.out.println(user.getLogonname()
                    + " check id:" + session.getId());
        }

        return "/singleLogin";
    }

    @RequestMapping(value = "/test/session")
    @ResponseBody
    public JsonResult<String> sessionTest() throws Exception {
        return new JsonResult<>("ok");
    }


}
